<?php

namespace app\api\controller;


use app\common\controller\Api;
use app\common\model\Apply as ModelApply;

/**
 * 预约
 * @ApiWeigh (200)
 */
class Apply extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [''];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];


    /**
     * 预约
     * @Author xy
     * @ApiTitle 
     * @ApiMethod (POST)
     * @ApiParams (name="apply_user", type="str", required=true, description="申请人")
     * @ApiParams (name="name", type="str", required=true, description="预约人姓名")
     * @ApiParams (name="num", type="integer", required=true, description="预约人数")
     * @ApiParams (name="applytime", type="str", required=true, description="预约时间")
     * @ApiParams (name="reason", type="str", required=false, description="预约需求")
     * @ApiParams (name="remark", type="str", required=false, description="备注")
     * @ApiReturnParams (name="code", type="integer", required=true, sample="0")
     * @ApiReturn ({'code':0,'msg':'返回成功'})
     */
    public function add()
    {

        $rej = $this->validate(
            $this->request->post(),
            [
                'apply_user' => 'require',
                'name'  => 'require',
                'num' => 'require|number|egt:1',
                'reason'  => 'require|max:255',
                'applytime' => 'require',
            ]
        );
        if (true !== $rej) {
            $this->responseMsg(1005, [], $rej);
        }
        $user = $this->auth->getUser();

        $data = $this->request->post();
        $data['user_id'] = $user->id;
        $data['acct_id'] = $user->acct_id;

        $res = new ModelApply($data);
        $res->allowField(true)->save();
        $this->responseMsg(0, $res->id);
    }






    /**
     * 列表
     * @Author xy
     * @ApiTitle 
     * @ApiMethod (POST)
     * @ApiParams (name="status", type="str", required=true, description="状态:0=待审核,1=已通过,2=未通过")
     * @ApiParams (name="page", type="integer", required=false, description="页码")
     * @ApiReturnParams (name="code", type="integer", required=true, sample="0")
     * @ApiReturn ({'code':0,'msg':'返回成功'})
     */
    public function list()
    {


        $user = $this->auth->getUser();
        $rej = $this->validate(
            $this->request->post(),
            [
                'status'  => 'require|in:0,1,2'
            ]
        );
        if (true !== $rej) {
            $this->responseMsg(1005, [], $rej);
        }
        $res = ModelApply::where('user_id', $user->id)->where('status', $this->request->post('status'))->order('id desc')->paginate(10);
        $this->responseMsg(0, $res);
    }



    /**
     * 详情
     * @Author xy
     * @ApiTitle 
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="integer", required=true, description="约局ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':0,'msg':'返回成功'})
     */
    public function detail()
    {
        $rej = $this->validate(
            $this->request->post(),
            [
                'id'  => 'require|number'
            ]
        );
        if (true !== $rej) {
            $this->responseMsg(1005, [], $rej);
        }
        $act = ModelApply::get($this->request->post('id'));
        if (!$act) {
            $this->responseMsg(1005, [], '未找到该数据');
        }

        $res = $act->toArray();
        $this->responseMsg(0, $res);
    }
}
